package com.example.web.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.common.Interface.cart.entity.vo.CartItemVO;
import com.example.common.Interface.product.entity.dto.*;
import com.example.common.Interface.product.service.ProductService;
import com.example.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @DubboReference(check = false)
    private ProductService productService;

    @PostMapping("/insert")
    public ResponseResult<InsertProductRespDTO> insert(@RequestBody InsertProductReqDTO request) {
        if (request == null) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.insert(request));
    }

    @PostMapping("/insert/batch")
    public ResponseResult<List<InsertProductRespDTO>> insertList(@RequestBody List<InsertProductReqDTO> request) {
        if (CollectionUtils.isEmpty(request)) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.insertList(request));
    }

    @DeleteMapping("/delete")
    public ResponseResult<DeleteProductRespDTO> delete(@RequestBody DeleteProductReqDTO request) {
        if (request == null) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.delete(request));
    }

    @PostMapping("/search")
    public ResponseResult<SearchProductsRespDTO> search(@RequestBody SearchProductsReqDTO request) {
        return ResponseResult.success(productService.search(request));
    }

    @PutMapping("/update")
    public ResponseResult<UpdateProductRespDTO> update(@RequestBody UpdateProductReqDTO request) {
        if (request == null) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.update(request));
    }

    @PostMapping("/get")
    public ResponseResult<GetProductRespDTO> get(@RequestBody GetProductReqDTO request) {
        if (request == null) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.get(request));
    }

    @PostMapping("/batch/get")
    public ResponseResult<List<GetProductRespDTO>> getList(@RequestBody List<GetProductReqDTO> request) {
        if (CollectionUtils.isEmpty(request)) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.getList(request));
    }

    @PostMapping("/stock/deduct")
    public ResponseResult<Boolean> deductStock(@RequestBody List<CartItemVO> request) {
        if (CollectionUtils.isEmpty(request)) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.deductStock(request));
    }

    @PostMapping("/stock/add")
    public ResponseResult<Boolean> addStock(@RequestBody List<CartItemVO> request) {
        if (CollectionUtils.isEmpty(request)) {
            return ResponseResult.fail("请求为空");
        }
        return ResponseResult.success(productService.addStock(request));
    }

    @PostMapping("/list")
    public ResponseResult<ListProductsRespDTO> list(@RequestBody ListProductsReqDTO request) {
        return ResponseResult.success(productService.list(request));
    }
}
